package view.client;

import model.Hearer;
import model.game.Player;
import java.util.ArrayList;
import model.game.Skill;
import model.game.Item;
import controller.link.battle.PlayerAttack;
import controller.link.RefreshGameData;
import controller.link.battle.UseConsumable;
import javax.swing.JTextArea;

public class Console {

    private Hearer sc;
    private ArrayList<Player> players;
    private ArrayList<Skill> skills;
    private ArrayList<Item> items;
    public ArrayList<Item> items2;
    private Frame frame;
    private PanelGame pg;
    private PanelBattle pb;
    private DialogHeal dh;
    private DialogSeller ds;
    private Console thisCsl;
    private int momentum;    // momento 0 = cenário // momento 1 = batalha //

    public Console(Frame frame, PanelGame pg, Hearer sc) {
        this.frame = frame;
        this.thisCsl = this;
        this.pg = pg;
        this.sc = sc;
        momentum = 0;
        RefreshGameData rgd = new RefreshGameData(sc);
        //Message newMsg = new Message(4);
        //sc.send(newMsg);
    }

    public Hearer getHearer() {
        return getSc();
    }
    public void closeDialog(){
        dh.dispose();
    }
    public JTextArea getTxtArea(){
        if (momentum ==1){
            return pb.getTxtArea();
        }else{
            return pg.getTxtArea();
        }
    }
    public void openSeller(ArrayList<Item> items2){
        this.items2 = items2;
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                setDs(new DialogSeller(new javax.swing.JFrame(), thisCsl, pg, thisCsl.items2, sc));
                getDs().addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                    }
                });
                getDs().setVisible(true);
            }
        });
    }
    public void initDialog() {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                dh = new DialogHeal(new javax.swing.JFrame(), sc);
                dh.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                    }
                });
                dh.setVisible(true);
            }
        });
    }

    public void inputedText(String text) {
        if (momentum == 0) {
            if (!text.substring(0, 1).equals("/")) {
                return;
            }
            if (text.equals("/lutar")) {
                pg.getTxtArea().append("Você quer lutar, mas não sabe com quem...(lutar nomeDoPersonagem)\n");
            } else if (findItem(text.substring(1)) != null) {
                //Message newMsg = new Message(27,text.substring(1), null);
                //getSc().send(newMsg);
            } else if (text.contains("/lutar com")) {
                try {
                    //Message newMsg = new Message(5, text.substring(11));
                    //getSc().send(newMsg);
                    getPg().getListAction().setEnabled(false);
                } catch (Exception e) {
                }

            }
        }
        try {
            if (momentum == 1) {
                if (findSkill(text.substring(1)) != null) {
                    PlayerAttack pa = new PlayerAttack(sc, findSkill(text.substring(1)));
                    momentum = 0;
                    getPb().getListAction().setEnabled(false);
                } else if (findItem(text.substring(1)) != null) {
                    UseConsumable uc = new UseConsumable(sc, findItem(text.substring(1)));
                    momentum = 0;
                    getPb().getListAction().setEnabled(false);
                }  else if (findSkill(text.substring(1)) == null) {
                    pb.getTxtArea().append("Você não possui essa skill\n");
                }
            }
        } catch (Exception e) {
            pb.getTxtArea().append("Comando inválido\n");
        }


    }

    public Skill findSkill(String name) {
        for (int i = 0; i < getSkills().size(); i++) {
            if (getSkills().get(i).getName().toLowerCase().equals(name.toLowerCase())) {
                return getSkills().get(i);
            }
        }
        return null;
    }

    public Item findItem(String name) {
        for (int i = 0; i < getItems().size(); i++) {
            if (getItems().get(i).getName().toLowerCase().equals(name.toLowerCase())) {
                return getItems().get(i);
            }
        }
        return null;
    }

    public void refresh(String status) {
        if (pg!=null){
            pg.getTxtArea().append(status + "\n");
        }
        if (pb!=null){
            pb.getTxtArea().append(status + "\n");
        }
    }

    public void outputText(String str) {
        if (pg!=null){
            pg.getTxtArea().append("Server: " + str + "\n");
        }
        if (pb!=null){
            pb.getTxtArea().append("Server: " + str + "\n");
        }
    }

    public int getMomentum() {
        return momentum;
    }

    public void setMomentum(int momentum) {
        this.momentum = momentum;
    }

    public Hearer getSc() {
        return sc;
    }

    public ArrayList<Player> getPlayers() {
        return players;
    }

    public ArrayList<Skill> getSkills() {
        return skills;
    }

    public ArrayList<Item> getItems() {
        return items;
    }

    public Frame getFrame() {
        return frame;
    }

    public PanelGame getPg() {
        return pg;
    }

    public PanelBattle getPb() {
        return pb;
    }

    public void setPg(PanelGame pg) {
        this.pg = pg;
    }

    public void setPb(PanelBattle pb) {
        this.pb = pb;
    }

    public void setPlayers(ArrayList<Player> players) {
        this.players = players;
    }

    public void setSkills(ArrayList<Skill> skills) {
        this.skills = skills;
    }

    public void setItems(ArrayList<Item> items) {
        this.items = items;
    }

    public DialogHeal getDh() {
        return dh;
    }

    public void setDh(DialogHeal dh) {
        this.dh = dh;
    }

    public DialogSeller getDs() {
        return ds;
    }

    public void setDs(DialogSeller ds) {
        this.ds = ds;
    }
}
